package com.myapp.foodapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.myapp.foodapp.bean.FoodListBean;
import com.myapp.foodapp.net.NetUtil;

import org.json.JSONObject;

import java.util.List;



public class FoodListActivity extends AppCompatActivity {
    private static final String TAG = "FoodListActivity";
    ListView mLvFoods;
    private List<FoodListBean.DataBean> list;
    MyAdapter myAdapter ;
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list!=null?list.size():0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final FoodListBean.DataBean dataBean = list.get(position);
            View view = View.inflate(getApplicationContext(),
                    R.layout.list_item, null);
            TextView tvFoodName = view.findViewById(R.id.tv_food_name);
            tvFoodName.setText(dataBean.getName());
            tvFoodName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(FoodListActivity.this,FoodDetailActivity.class);
                    int foodId = dataBean.getId();
                    intent.putExtra("foodId",foodId);
                    Log.e(TAG,"onClick:" +foodId);
                    startActivity(intent);
                }
            });
            tvFoodName.setText(dataBean.getName());
            return view;
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_list);
        initView();
        initEvent();
        initData();
        updateView();
    }

    private void updateView() {
        myAdapter = new MyAdapter();
        mLvFoods.setAdapter(myAdapter);
    }

    private void initData() {
        final String url = "http://10.2.64.75:8080/foodserver/food/list";
        new Thread(new Runnable() {
            @Override
            public void run() {
                String data = NetUtil.getData(new JSONObject(), url);
                Gson gson = new Gson();
                final FoodListBean foodListBean
                        = gson.fromJson(data, FoodListBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        list = foodListBean.getData();
                        updateView();
                    }
                });
            }
        }).start();

    }

    private void initEvent() {

    }

    private void initView() {
        mLvFoods = findViewById(R.id.lv_foods);
    }
}